package zhjb.platform.module.urlcollect.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import zhjb.platform.dao.DCriteriaPageSupport;
import zhjb.platform.module.urlcollect.bean.URLType;
import zhjb.platform.module.urlcollect.bean.UrlCollect;
import zhjb.platform.web.service.BaseService;

public class UrlCollectService extends BaseService {
	/**
	 * 获取所有的URLType
	 * 
	 * @return
	 */
	public List<URLType> findAllURLType() {
		return dao.findAllByIObjectCType(URLType.class);
	}

	/**
	 * 根据地址和分类找到数量
	 * 
	 * @param url
	 * @param type
	 * @return
	 */
	public UrlCollect findCollectCount(String url, String type, String user) {
		List<UrlCollect> list = dao.findAllByCriteria(DetachedCriteria.forClass(UrlCollect.class).add(Restrictions.eq("url", url)).add(Restrictions.eq("createuser", user)).add(Restrictions.eq("urltype", type)));
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 更新數據
	 * 
	 * @param collect
	 */
	public void update(UrlCollect collect) {
		dao.updateIObject(collect);
	}

	/**
	 * 保存數據
	 * 
	 * @param collect
	 */
	public void addCollect(UrlCollect collect) {
		dao.saveIObject(collect);
	}

	/**
	 * 
	 * @param page
	 * @param pagesize
	 * @param user
	 * @return
	 */
	public DCriteriaPageSupport<UrlCollect> listUrlCollect(int page, int pagesize, String user, String query) {
		Criterion criterion = null;
		if (query != null && query.trim().length() > 0) {
			String[] querys = query.split("[ ]{1,}");
			for (String term : querys) {
				if (criterion == null) {
					criterion = Restrictions.like("urltitle", term, MatchMode.ANYWHERE);
				} else {
					criterion = Restrictions.or(criterion, Restrictions.like("urltitle", term, MatchMode.ANYWHERE));
				}
			}
		}

		DCriteriaPageSupport<UrlCollect> list = null;
		if (criterion != null) {
			list = dao.findPageByCriteria(DetachedCriteria.forClass(UrlCollect.class).add(criterion).addOrder(Order.desc("createtime")), pagesize, page);
		} else {
			list = dao.findPageByCriteria(DetachedCriteria.forClass(UrlCollect.class).addOrder(Order.desc("createtime")), pagesize, page);
		}

		List<URLType> types = dao.findAllByCriteria(DetachedCriteria.forClass(URLType.class));
		if (list != null && !list.isEmpty()) {
			for (UrlCollect collect : list) {
				for (URLType type : types) {
					if (type.getId().equals(collect.getUrltype())) {
						collect.setTypeShow(type.getTypeName());
					}
				}
			}
		}
		return list;
	}

	/**
	 * 删除数据u
	 * 
	 * @param ids
	 */
	public void delete(String[] ids) {
		if (ids != null && ids.length > 0) {
			Set<String> idset = new HashSet<String>(Arrays.asList(ids));
			StringBuilder sbSQL = new StringBuilder();
			for (String id : idset) {
				if (sbSQL.length() > 0) {
					sbSQL.append("','");
				}
				sbSQL.append(id);
			}
			sbSQL.append("')");
			sbSQL.insert(0, "delete from UrlCollect where id in ('");
			dao.execByHQL(sbSQL.toString());
		}
	}
}
